// 导航守卫逻辑
import router from './router/index'
import store from './store/index'
// 导航守卫
const whiteList = ['/login', '/404']
router.beforeEach(async (to, from, next) => {
  if (store.getters.token) {
    // 登录了
    if (to.path === '/login') {
      next('/')
    } else {
      // 登录了，去其他界面
      if (!store.getters.userId) {
        // 没有用户资料获取，
        const { roles } = await store.dispatch('user/getUserInfoAction')
        const userAsyncRoutes = await store.dispatch('permissions/filterRoutes', roles.menus)
        // 通过router.addRoutes方法，动态添加路由映射配置
        router.addRoutes(userAsyncRoutes)
        next(to.path)
      }
      next()
    }
  } else {
    // 未登录
    if (whiteList.includes(to.path)) {
      // 在白名单中
      next()
    } else {
      next('/login')
    }
  }
})
router.afterEach((to, from) => {})
